package com.resign.carl.state.api;


import com.resign.carl.state.api.impl.*;
import lombok.Data;
import lombok.ToString;

/**
 * Created by carl.zhao on 2016/6/16.
 */
@Data
@ToString
public class GumballMachine {
    // 库存为0
    private State soldOutState;
    // 金钱不足
    private State noQuarterState;
    // 金钱足够
    private State hasQuarterState;
    // 售完
    private State soldState;
    // winner
    private State winnerState;

    private State state = soldState;

    private int count = 0;

    public GumballMachine(int numberGumballs){
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        this.count = numberGumballs;
        if(numberGumballs > 0){
            state = noQuarterState;
        }
    }

    public void insertQuarter(){
        state.insertQuarter();
    }

    public void ejectQuarter(){
        state.ejectQuarter();
    }

    public void turnCrank(){
        state.turnCrank();
        state.dispense();
    }

    public void setState(State state) {
        this.state = state;
    }

    public void releaseBall(){
        System.out.println("A gumball comes rolling out the slot...");
        if(0 != count){
            count = count -1;
        }
    }

    public void refill(int count){
        this.count = count;
        state = noQuarterState;
    }
}
